package com.example.bbgds.test;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bbgds.test.itemanimator.MyAnimat2;
import com.example.bbgds.test.itemanimator.MyItemAnimator;
import com.example.bbgds.test.itemanimator.SlideInOutLeftItemAnimator;
import com.example.bbgds.test.itemanimator.SlideInOutRightItemAnimator;
import com.example.bbgds.test.itemanimator.SlideScaleInOutRightItemAnimator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bbgds on 2016/3/7.
 */
public class LayoutBottom2 extends RelativeLayout{
    private RecyclerView mRecyclerView;
    private PersonAdapter mPersonAdapter;
    private MyAnimat2 mMyAnimat2;
    private MyItemAnimator mMyItemAnimator;
    private VoiceActivity2 mVoiceActivity2;

    private static List<DataItem> mList = new ArrayList<DataItem>();

    public void setVoiceActivity2(VoiceActivity2 mvoice){
        mVoiceActivity2 = mvoice;
    }
    public LayoutBottom2(Context context) {
        this(context, null);
    }

    public LayoutBottom2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private boolean isAdd = false;
    private int addCount = 1;
    public LayoutBottom2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.layout3, this);
        mRecyclerView = (RecyclerView) findViewById(R.id.listview);
        mRecyclerView.setLayoutManager(new MyLinearLayoutManager(context));
        mPersonAdapter = new PersonAdapter();
        mRecyclerView.setAdapter(mPersonAdapter);
        mMyAnimat2 = new MyAnimat2(new MyAnimat2.OnAddFinishedListener(){

            @Override
            public void onAddFinished(RecyclerView.ViewHolder item) {
//                addCount--;
//                Log.e("zhu", "addCount=" + addCount);
//                RuntimeException ex = new RuntimeException();
//                ex.fillInStackTrace();
//                Log.e("zhu", "s", ex);
//                if (addCount == 0) {
//                    Log.e("zhu", "onAddFinished");
//                    if (isAdd) {
//                        mVoiceActivity2.addList();
//                        isAdd = false;
//                    }
//                }
            }
        });
        mMyItemAnimator = new MyItemAnimator(this);
        mRecyclerView.setItemAnimator(mMyAnimat2);
    }


    public RecyclerView getRecyclerView(){
        return mRecyclerView;
    }

    public void addItem(DataItem item){
        addCount = 1;
        isAdd = true;
        addItemToList(item);
    }

    public void addItemToList(DataItem item){
        Log.e("zhu", "add list");
        addCount = 1;
        mList.add(0, item);
        mPersonAdapter.notifyItemInserted(0);
        mRecyclerView.getLayoutManager().scrollToPosition(0);
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e("zhu", "run check");
                mMyAnimat2.isRunning(new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {
                    @Override
                    public void onAnimationsFinished() {
                        Log.e("zhu", "onAnimationsFinished=" + addCount);
                        mVoiceActivity2.addList();
                    }
                });
            }
        }, 250);
    }

    public void addItemToList(List<DataItem> items) {
        Log.e("zhu", "add list");

        mList.addAll(0, items);
        mPersonAdapter.notifyItemInserted(0);
//        mPersonAdapter.notifyDataSetChanged();
        mRecyclerView.getLayoutManager().scrollToPosition(0);
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e("zhu", "run check");
                mMyAnimat2.isRunning(new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {
                    @Override
                    public void onAnimationsFinished() {
                        Log.e("zhu", "onAnimationsFinished=" + addCount);
                    }
                });
            }
        }, 250);
    }


    public class PersonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType == DataItem.TYPE_TAG){
                RelativeLayout layout = new RelativeLayout(parent.getContext());
                LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                View textView = View.inflate(parent.getContext(), R.layout.layout_tag, null);
                textView.setLayoutParams(lp);
                layout.addView(textView);
                layout.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                return new TagViewHolder(layout, textView);
            }else if(viewType == DataItem.TYPE_TEXT){
                RelativeLayout layout = new RelativeLayout(parent.getContext());
                LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                TextView textView = VoiceActivity.getTextView(parent.getContext(), null);
                textView.setLayoutParams(lp);
                layout.addView(textView);
                layout.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                return new PersonViewHolder(layout, textView);
            }else if(viewType == DataItem.TYPE_MID){
                RelativeLayout layout = new RelativeLayout(parent.getContext());
                LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                TextView textView = VoiceActivity.getTextView(parent.getContext(), null);
                textView.setLayoutParams(lp);
                layout.addView(textView);
                layout.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                return new MidMyViewHolder(layout, textView);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
            layoutParams.topMargin = 50;
//            if (position == 0) {
//                layoutParams.bottomMargin = 50;
//            } else {
//                layoutParams.bottomMargin = 0;
//            }
            if(holder instanceof PersonViewHolder){
                ((PersonViewHolder)holder).initData(position);
            }else if(holder instanceof TagViewHolder){
                ((TagViewHolder)holder).initData(position);
            }else if(holder instanceof MidMyViewHolder){
                ((MidMyViewHolder)holder).initData(position);
            }
        }

        @Override
        public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
            super.onViewDetachedFromWindow(holder);
            holder.itemView.clearAnimation();
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        @Override
        public int getItemViewType(int position) {
            return mList.get(position).mType;
        }

        private static final long DURATION = 300;

        protected void setAnimation(View viewToAnimate, int position) {
            ObjectAnimator translation = ObjectAnimator.ofFloat(viewToAnimate, "translationY", 200, 0);
            translation.setDuration(DURATION);
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(viewToAnimate, "scaleX", 0, 1f);
            scaleX.setDuration(DURATION);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(viewToAnimate, "scaleY", 0, 1f);
            scaleY.setDuration(DURATION);
            AnimatorSet nextAnimatorSet = new AnimatorSet();
            nextAnimatorSet.playTogether(scaleX, scaleY, translation);
            nextAnimatorSet.start();
        }

        protected void setAnimation2(View viewToAnimate, int position) {
            ObjectAnimator translation = ObjectAnimator.ofFloat(viewToAnimate, "translationY", viewToAnimate.getHeight(), 0);
            translation.start();
        }
    }


    public static class PersonViewHolder extends MyViewHolder{
        private TextView textView;
        private DataItem mDataItem;
        public PersonViewHolder(View itemView, TextView view) {
            super(itemView);
            textView = view;
        }

        public void initData(int position){
            mDataItem = mList.get(position);
            if(mDataItem.isVisible){
                textView.setVisibility(View.VISIBLE);
            }else{
                textView.setVisibility(View.INVISIBLE);
            }
            textView.setText(mDataItem.mText);
            LayoutParams lp = (LayoutParams)textView.getLayoutParams();
            lp.leftMargin = 0;
            lp.rightMargin = 0;
            itemView.setTranslationX(0);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//            if(position == 0){
//                lp.leftMargin = 100;
//                lp.rightMargin = 100;
//                lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
//            }else{
                    lp.leftMargin = 200;
                    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

//            }
            textView.setLayoutParams(lp);
        }

        @Override
        public boolean isRight() {
            return true;
        }

        @Override
        public boolean canMoveX() {
            LayoutParams lp = (LayoutParams)textView.getLayoutParams();
            if(lp.getRules()[RelativeLayout.CENTER_HORIZONTAL] == RelativeLayout.TRUE && itemView.getTranslationX() == 0){
                return true;
            }
            return false;
        }
    }


    public static class MidMyViewHolder extends MyViewHolder{
        private TextView textView;
        private DataItem mDataItem;
        public MidMyViewHolder(View itemView, TextView view) {
            super(itemView);
            textView = view;
        }

        public void initData(int position){
            mDataItem = mList.get(position);
            if(mDataItem.isVisible){
                textView.setVisibility(View.VISIBLE);
            }else{
                textView.setVisibility(View.INVISIBLE);
            }
            textView.setText(mDataItem.mText);
            LayoutParams lp = (LayoutParams)textView.getLayoutParams();
            lp.leftMargin = 0;
            lp.rightMargin = 0;
            itemView.setTranslationX(0);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            lp.removeRule(RelativeLayout.CENTER_HORIZONTAL);
            if(position == 0){
                lp.leftMargin = 100;
                lp.rightMargin = 100;
                lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
            }else{
                lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            }
            textView.setLayoutParams(lp);
        }

        @Override
        public boolean isRight() {
            return true;
        }

        @Override
        public boolean canMoveX() {
            LayoutParams lp = (LayoutParams)textView.getLayoutParams();
            if(lp.getRules()[RelativeLayout.CENTER_HORIZONTAL] == RelativeLayout.TRUE && itemView.getTranslationX() == 0){
                return true;
            }
            return false;
        }
    }

    public static class TagViewHolder extends MyViewHolder{
        private View textView;
        private View itemView;
        private DataItem mDataItem;
        public TagViewHolder(View itemView, View tagView) {
            super(itemView);
            this.itemView = itemView;
            textView = tagView;
        }

        public void initData(int position){
            mDataItem = mList.get(position);
            LayoutParams lp = (LayoutParams)textView.getLayoutParams();
            lp.leftMargin = 0;
            lp.rightMargin = 0;
            itemView.setTranslationX(0);
//            if(position == 0){
//                lp.leftMargin = 100;
//                lp.rightMargin = 100;
//                lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
//            }else{
//            }
            textView.setLayoutParams(lp);
        }

        @Override
        public boolean isRight() {
            return false;
        }

        @Override
        public boolean canMoveX() {
            LayoutParams lp = (LayoutParams)textView.getLayoutParams();
            if(lp.getRules()[RelativeLayout.CENTER_HORIZONTAL] == RelativeLayout.TRUE && itemView.getTranslationX() == 0){
                return true;
            }
            return false;
        }
    }

    public static class MyLinearLayoutManager extends LinearLayoutManager {

        public MyLinearLayoutManager(Context context) {
            super(context);
            this.setReverseLayout(true);
        }

    }

    public static abstract class MyViewHolder extends RecyclerView.ViewHolder{

        public MyViewHolder(View itemView) {
            super(itemView);
        }

        public abstract boolean isRight();

        public abstract boolean canMoveX();
    }

    public boolean isBottom() {
        LinearLayoutManager lm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
        if (lm.findViewByPosition(lm.findFirstVisibleItemPosition()).getTop() == 0 && lm.findFirstVisibleItemPosition() == 0) {
            return true;
        }
        return false;
    }

    private void scrollToBottom(){
        mRecyclerView.getLayoutManager().scrollToPosition(0);
    }
}
